package com.ecommerce.model;

import java.util.Date;

public class Coupon {
    private int id;
    private String code;
    private String description;
    private double discountAmount;
    private double minOrderAmount;
    private Date startDate;
    private Date endDate;
    private int usageLimit;
    private int usedCount;
    private String status;

    public Coupon() {}

    public Coupon(String code, String description, double discountAmount,
                  double minOrderAmount, Date startDate, Date endDate, int usageLimit) {
        this.code = code;
        this.description = description;
        this.discountAmount = discountAmount;
        this.minOrderAmount = minOrderAmount;
        this.startDate = startDate;
        this.endDate = endDate;
        this.usageLimit = usageLimit;
        this.usedCount = 0;
        this.status = "active";
    }

    // Getters and Setters
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }

    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }

    public double getDiscountAmount() { return discountAmount; }
    public void setDiscountAmount(double discountAmount) { this.discountAmount = discountAmount; }

    public double getMinOrderAmount() { return minOrderAmount; }
    public void setMinOrderAmount(double minOrderAmount) { this.minOrderAmount = minOrderAmount; }

    public Date getStartDate() { return startDate; }
    public void setStartDate(Date startDate) { this.startDate = startDate; }

    public Date getEndDate() { return endDate; }
    public void setEndDate(Date endDate) { this.endDate = endDate; }

    public int getUsageLimit() { return usageLimit; }
    public void setUsageLimit(int usageLimit) { this.usageLimit = usageLimit; }

    public int getUsedCount() { return usedCount; }
    public void setUsedCount(int usedCount) { this.usedCount = usedCount; }

    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }

    // 业务方法
    public boolean isValid() {
        Date now = new Date();
        return "active".equals(status) &&
                now.after(startDate) &&
                now.before(endDate) &&
                usedCount < usageLimit;
    }

    public boolean canApply(double orderAmount) {
        return isValid() && orderAmount >= minOrderAmount;
    }

    public double applyDiscount(double orderAmount) {
        if (canApply(orderAmount)) {
            return Math.max(0, orderAmount - discountAmount);
        }
        return orderAmount;
    }

    @Override
    public String toString() {
        return String.format("优惠码: %s | 描述: %s | 折扣: ¥%.2f | 最低消费: ¥%.2f | 状态: %s",
                code, description, discountAmount, minOrderAmount,
                isValid() ? "有效" : "无效");
    }

    public String toDetailedString() {
        return String.format(
                "优惠码ID: %d\n优惠码: %s\n描述: %s\n折扣金额: ¥%.2f\n最低消费: ¥%.2f\n" +
                        "开始时间: %s\n结束时间: %s\n使用限制: %d/%d\n状态: %s",
                id, code, description, discountAmount, minOrderAmount,
                startDate, endDate, usedCount, usageLimit, status);
    }
}